<template>
    <div class="app-container">
        <!-- 巡查反馈管理 -->
        <el-row>
            <el-col :span="24">
                <el-form ref="queryParamsRef" :inline="true" :model="queryParams">
                    <el-row>
                        <!-- <el-col :span="10">
                            <el-form-item label="被巡查单位">
                                <el-tree-select 
                                v-model="queryParams.bxcunitId"
                                :data="UnitOptions" 
                                :props="{ value: 'deptId', label: 'deptName', children: 'children'}"
                                placeholder="请选择被巡查单位"
                                check-strictly
                                default-expand-all
                                clearable
                                style="width:260px;margin-right: 15px;"
                                @change="getLists"
                                 />
                                <el-checkbox @change="getLists" v-model="isunitIdChild" label="含下级" size="large" />
                            </el-form-item>
                        </el-col> -->
                        <el-col :span="6">
                            <el-form-item label="巡查场所">
                                <el-input v-model="queryParams.xcplace" style="width: 260px" clearable @clear="getLists"
                                    @keydown.enter.prevent="getLists" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="6">
                            <el-form-item label="巡查结果">
                                <el-select v-model="queryParams.res" placeholder="选择巡查结果" @change="getLists"
                                    style="width: 260px">
                                    <el-option v-for="item in nj_xcresult" :key="item.value" :label="item.label"
                                        :value="item.value" />
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="7">
                            <el-form-item label="巡查类型" label-width="88px">
                                <el-select v-model="queryParams.xctype" placeholder="选择巡查类型" @change="getLists"
                                    style="width: 260px" clearable>
                                    <el-option v-for="item in nj_xc_type" :key="item.value" :label="item.label"
                                        :value="item.value" />
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-form-item>
                            <el-button type="primary" @click="getLists">
                                <el-icon class="me-1">
                                    <Search />
                                </el-icon>
                                查询
                            </el-button>
                        </el-form-item>
                        <el-form-item>
                            <el-button @click="handleRest(queryParamsRef)">
                                <el-icon class="me-1">
                                    <Refresh />
                                </el-icon>
                                重置
                            </el-button>
                        </el-form-item>
                    </el-row>
                </el-form>
            </el-col>
        </el-row>
        <el-divider class="mt-0 mb-3" />
        <el-table :data="tableData" v-loading="loading"
        @row-dblclick="handleRowDblClick"
            stripe
            :row-style="tableRowStyle"
        border>
            <el-table-column prop="xcunitId" label="巡查单位" />
            <el-table-column prop="bxcunitId" label="被巡查单位" />
            <el-table-column prop="xcplace" label="巡查场所" />
            <el-table-column prop="xcuserId" label="核定人" />
            <el-table-column prop="xcdatetime" label="巡查时间" />
            <!-- <el-table-column prop="adddate" label="巡查案件-接口没返回"/> -->
            <!-- <el-table-column prop="adddate" label="巡查问题-没看到返回"/> -->
            <el-table-column label="巡查结果" prop="res">
                <template #default="scope">
                    <dict-tag :options="nj_xcresult" :value="scope.row.res" />
                </template>
            </el-table-column>
            <el-table-column prop="stateText" label="巡查结果状态" width="110" />
            <el-table-column label="操作" width="140" align="center">
                <template #default="scope">
                    <el-button v-if="scope.row.state == 0" link size="small" type="primary"
                        @click="CheckFeedbackManageDetailRef.showDetail(scope.row, '巡查反馈', 'Edit')">
                        <el-icon class="me-1">
                            <Edit />
                        </el-icon>
                        反馈
                    </el-button>
                    <el-button type="primary" size="small" link
                        @click="CheckFeedbackManageDetailRef.showDetail(scope.row, '巡查反馈详情', 'See')">
                        <el-icon class="me-1">
                            <View />
                        </el-icon>
                        详情
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <pagination v-show="total > 0" v-model:page="paging.pageNum" v-model:limit="paging.pageSize" :total="total"
            @pagination="getLists" />
        <CheckFeedbackManageDetail ref="CheckFeedbackManageDetailRef" @getList="getLists" />
    </div>
</template>

<script setup>
import { getCurrentInstance } from "vue";
const { proxy } = getCurrentInstance()
import { listDept } from "@/api/system/dept";
import { returnList } from "@/api/monitors/monitors.js";
import CheckFeedbackManageDetail from "@/views/OnlineCheck/CheckFeedbackManage/components/CheckFeedbackManageDetail.vue";
import { getUserProfile } from "@/api/system/user";
import PageEnum from "@/enum/PageEnum.js";
const { nj_yuan_type, nj_xcresult, nj_xc_type } = proxy.useDict('nj_yuan_type', 'nj_xcresult', 'nj_xc_type')

const loading = ref(false)
const total = ref(0)
const CheckFeedbackManageDetailRef = ref({})

const UnitOptions = ref([])
const isunitIdChild = ref(false)
const paging = ref({
    // 页码
    pageNum: 1,
    // 分页数量
    pageSize: PageEnum.SIZE
})

const queryParams = ref({
    xcunitId: '',
    xcplace: '',         // 巡查场所
    res: '1',        // 巡查结果
    xctype: ''
})

const tableData = ref([{}])

onMounted(() => {
    getUserInfo()
    getDeptTree()
})
// 斑马纹样式函数
const tableRowStyle = ({ row, rowIndex }) => {
    if (rowIndex % 2 === 0) {
        return { backgroundColor: '#e6ecff' }; // 偶数行背景色
    }
    return {}; // 奇数行默认透明
}
const handleRowDblClick = (row) => {
    CheckFeedbackManageDetailRef.value.showDetail(row, '巡查反馈详情', 'See')
}
// 获取个人信息
const getUserInfo = async () => {
    await getUserProfile().then(res => {
        queryParams.value.xcunitId = res.data.unitId
    })
    getLists()
}
/** 查询部门下拉树结构 */
const getDeptTree = async () => {
    await listDept().then(res => {
        UnitOptions.value = res.data.filter(
            item => item.isUnit === true
        );
        UnitOptions.value = proxy.handleTree(UnitOptions.value, "deptId");
    });
}
// 获取列表
const getLists = async () => {
    // await getUserProfile().then(res => {
    //     queryParams.value.xcunitId = res.data.unitId
    // })
    loading.value = true

    const res = await returnList(queryParams.value, paging.value)
    tableData.value = res.rows
    total.value = res.total
    loading.value = false
}


// 重置搜索
const handleRest = async () => {
    queryParams.value.bxcunitId = ''
    queryParams.value.isunitIdChild = ''
    queryParams.value.xcplace = ''
    queryParams.value.res = ''
    queryParams.value.xctype = ''

    getLists()
}
</script>

<style scoped lang="scss">
/* 保留斑马纹的同时添加悬停效果 */
:deep().el-table--striped .el-table__body tr.el-table__row--striped:hover td {
  background-color: #a5b8f4; /* 悬停时的红色 */
}

:deep().el-table__body tr:hover td {
  background-color: #a5b8f4; /* 悬停时的红色 */
}
</style>